<template>
  <Page>
    <!-- 文件上传区域 -->
    <div style="margin-top: 20px">
      <h3>文件上传测试</h3>
      <el-button @click="simulateFileUpload">模拟文件上传</el-button>
    </div>

    <!-- 文件操作按钮 -->
    <div style="margin-top: 20px">
      <el-button @click="simulatePreview">模拟预览</el-button>
      <el-button @click="simulateDownload">模拟下载</el-button>
      <el-button @click="simulateRetry">模拟重试</el-button>
    </div>

    <!-- 测试结果 -->
    <div style="margin-top: 20px">
      <h3>测试结果</h3>
      <pre>{{ testResults }}</pre>
    </div>

    <!-- 分片上传组件 -->
    <Upload ref="uploadRef" />
  </Page>
</template>

<script setup>
import { ref } from 'vue'
import Page from '@/components/page/index'
import Upload from '@/components/upload/index' // 引入分片上传组件

// 定义 Refs
const uploadRef = ref(null)
const testResults = ref('')

/**
 * 格式化日志输出
 */
function log(message) {
  testResults.value += `${message}\n`
}

/**
 * 模拟文件上传
 */
function simulateFileUpload() {
  const file = new File(['test content'], 'test-file.txt', { type: 'text/plain' })
  const mockEvent = {
    raw: file,
    uid: Date.now().toString()
  }

  if (uploadRef.value) {
    log('开始模拟文件上传...')
    uploadRef.value.handleFileChange(mockEvent)
  }
}

/**
 * 模拟文件预览
 */
function simulatePreview() {
  const mockFile = {
    name: 'example.jpg',
    url: 'https://via.placeholder.com/150'
  }

  if (uploadRef.value) {
    log('开始模拟文件预览...')
    uploadRef.value.handlePreview(mockFile)
  }
}

/**
 * 模拟文件下载
 */
function simulateDownload() {
  const mockFile = {
    name: 'example.pdf',
    url: 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf'
  }

  if (uploadRef.value) {
    log('开始模拟文件下载...')
    uploadRef.value.handleDownload(mockFile)
  }
}

/**
 * 模拟上传重试
 */
function simulateRetry() {
  const mockFile = {
    uid: Date.now().toString(),
    name: 'failed-file.txt',
    size: 1024 * 1024, // 1MB
    raw: new File(['test content'], 'failed-file.txt', { type: 'text/plain' }),
    percentage: 0,
    status: 'exception',
    chunks: 1,
    uploadedChunks: 0
  }

  if (uploadRef.value) {
    log('开始模拟上传重试...')
    uploadRef.value.retryUpload(mockFile)
  }
}
</script>

<style scoped>
/* 样式调整 */
pre {
  background-color: #f4f4f4;
  padding: 10px;
  border-radius: 5px;
  max-height: 300px;
  overflow-y: auto;
}
</style>